package game.bejeweled
{
	import com.yzy.tool.Utils;
	
	import flash.events.EventDispatcher;
	import flash.geom.Point;
	
	import effect.Effect;
	
	import event.ControlEvent;
	
	import models.BallModel;
	import models.BallPool;
	import models.ProfileModel;
	
	import starling.textures.Texture;
	
	import test.Test;
	
	

	public class CreateBalls extends EventDispatcher
	{
		private var block:BejeweledBlock;
		private var backdropTexture:Texture;
		private var ballPool:BallPool;
		//
		private var dropVeclength:int;//
		private var currNum:int;
		
		public function CreateBalls(block:BejeweledBlock,backdropTexture:Texture)
		{
			this.block=block;
			this.backdropTexture=backdropTexture;
			//
			ballPool=new BallPool();
			createBackDrops();
		}
		
		private function createBackDrops():void{
			for (var j:int = 0; j < ProfileModel.instant.row; j++) 
			{
				var columnVec:Vector.<BackDrop>=new Vector.<BackDrop>();
				for (var k:int = 0; k < ProfileModel.instant.column; k++) 
				{
					var backdrop:BackDrop=new BackDrop(backdropTexture);
					backdrop.y=j*backdrop.width;
					backdrop.x=k*backdrop.height;
					backdrop.rowNum=j;
					backdrop.column=k;
					block.addChild(backdrop);
					columnVec.push(backdrop);				
				}
				ProfileModel.instant.backdropVec.push(columnVec);
			}
		}
		
		public function sameBall():void{
			var sameBalls:Vector.<Point>=TestSameBall.sameBallVec();
//			trace("sameBalls="+sameBalls.length)
			if(sameBalls.length){
				ProfileModel.instant.AllowCtrl=false;
			}else{
				ProfileModel.instant.AllowCtrl=true;
			}
			for (var i:int = 0; i < sameBalls.length; i++) 
			{
				var currBallback:BackDrop=ProfileModel.instant.backdropVec[sameBalls[i].x][sameBalls[i].y];
//				Effect.instant.removeBallEvent(currBallback);
				
				Effect.instant.shootBallEff(currBallback,new Point(350,200));
				
				Effect.instant.addEventListener("removeball_complete",removeComplete);
			}
		}
		
		private function removeComplete():void
		{
//			trace("消除完成")
			this.dispatchEvent(new ControlEvent(ControlEvent.REMOVE_BALL_COMPLETE));
		}		
		
		
	
		
		
		public function createBall():void{
			//test
//			var l:String=Test.instant.createBallTestData.replace("\n\t\r");
//			var s:Array=l.split(",");
//			var i:int=s.length-1;
//			var randomNum:int=s[i++];
			//test
			for (var r:int = ProfileModel.instant.row-1; r >=0; r--) 
			{
				for (var c:int = ProfileModel.instant.column-1; c >=0; c--) 
				{
					var currBackDrop:BackDrop=ProfileModel.instant.backdropVec[r][c];
					if(!currBackDrop.isFull){
						
						
						var randomNum:int=com.yzy.tool.Utils.getRandom(0,ProfileModel.instant.ballModelVec.length-1);
//						var randomNum:int=1;
//						var randomNum:int=s[i--];
						var ballModale:BallModel=ProfileModel.instant.ballModelVec[randomNum];
						var newBall:Ball=BallPool.instant.getClass(ballModale);
						newBall.sign=randomNum;
						
						var upbackDrop:BackDrop=null;
//						
						var upNum:int=r-1;
						for (var j:int = upNum; j >=0; j--) 
						{
							
							if(ProfileModel.instant.backdropVec[j][c].isFull){
								upbackDrop=ProfileModel.instant.backdropVec[j][c];
								break;					
							}
						}
						
						if(upbackDrop!=null){
							currBackDrop.currBall=upbackDrop.currBall;
							upbackDrop.currBall=null;
							var moveBall:Ball=	upbackDrop.currBall;
							var movetoP:Point=new Point(currBackDrop.ballX,currBackDrop.ballY);
						}else{
							currBackDrop.currBall=newBall;
							newBall.x=currBackDrop.ballX;
							newBall.y=currBackDrop.ballY-block.height;
							block.addChild(newBall);
							
							moveBall=newBall;
							movetoP=new Point(newBall.x,currBackDrop.ballY);
						}
						
						Effect.instant.addEventListener("moveball_complete",createComplete);
						Effect.instant.moveBallEffect(moveBall,movetoP);
					}
				}
			}
		}
		
		private function createComplete():void
		{
			this.dispatchEvent(new ControlEvent(ControlEvent.CREATE_BALL_COMPLETE));
		}		
		
		
		
		//************************************************************************
		// utils
		//***********************************************************************
		
		
		
	}
}

